package com.asktgapp.user.fragment;

import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.asktgapp.R;
import com.asktgapp.base.BaseFragment;
import com.asktgapp.eventbus.HomeServiceItemEvent;
import com.asktgapp.eventbus.PayFinishEvent;

import org.greenrobot.eventbus.EventBus;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * 上门服务订单页面
 */

public class ServiceOrderFragment extends BaseFragment {
    @InjectView(R.id.viewpager)
    ViewPager viewPager;
    @InjectView(R.id.group)
    RadioGroup mRadioGroup;
    @InjectView(R.id.right)
    RadioButton mRight;
    @InjectView(R.id.left)
    RadioButton mLeft;
    @InjectView(R.id.myBack)
    TextView myBack;
    private MyPagerAdapter pagerAdapter;
    private int position;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        ViewGroup viewGroup = (ViewGroup) inflater.inflate(R.layout.fragmenet_question, container, false);
        ButterKnife.inject(this, viewGroup);
        return super.onCreateView(inflater, viewGroup, savedInstanceState);
    }

    @Override
    protected void initView(View rootView) {
        getBaseActivity().getToolbar().setVisibility(View.GONE);
        pagerAdapter = new MyPagerAdapter(getFragmentManager());
        viewPager.setAdapter(pagerAdapter);
        mLeft.setText("未支付");
        mRight.setText("已支付");

        mLeft.setChecked(true);
        viewPager.setCurrentItem(0);
        viewPager.setOffscreenPageLimit(1);
        mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                int position = group.indexOfChild(group.findViewById(checkedId));
                viewPager.setCurrentItem(position);
                HomeServiceItemEvent.postEvent(true);
            }
        });

        myBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getBaseActivity().onBackPressed();
            }
        });
    }

    @Override
    public void loadData() {

    }

    public class MyPagerAdapter extends FragmentPagerAdapter {

        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            switch (position) {
                case 0:
                    return ServiceOrderItemFragment.newInstance(0);
                case 1:
                    return ServiceOrderItemFragment.newInstance(1);
            }
            return null;
        }

        @Override
        public int getCount() {
            return 2;
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
    }
}
